package com.cloudkinto.service.board.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.board.JpBoardService;
import com.cloudkinto.service.storage.StorageService;
import com.cloudkinto.service.storage.vo.StorageListRes;
import com.cloudkinto.service.tailtrucks.TailTrucksService;
import com.cloudkinto.service.tailtrucks.vo.TailTrucksPageRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author zhangyy
 * @date 2020/10/29 14:24
 * @description:
 */
@Service
public class JpBoardServiceImpl implements JpBoardService {
    @Autowired
    private TailTrucksService tailTrucksService;
    @Autowired
    private FbaStorageDao fbaStorageDao;
    @Autowired
    private TrucksTemplateDao trucksTemplateDao;
    @Autowired
    private StorageService storageService;

    @Override
    public SingleResult getInfo() {
        Map map = new HashMap();
        List<Map> fbaStorageList = new ArrayList<>();
        QueryWrapper<FbaStorageDo> wrapper1 = new QueryWrapper<>();
        wrapper1.orderByAsc("fba_code");
        List<FbaStorageDo> fbaStorageDos = fbaStorageDao.selectList(wrapper1);
        if (!CollectionUtils.isEmpty(fbaStorageDos)) {
            fbaStorageDos.forEach(fbaStorageDo -> {
                Map map1 = new HashMap();
                map1.put("id", fbaStorageDo.getId());
                map1.put("value", fbaStorageDo.getFbaCode());
                fbaStorageList.add(map1);
            });
        }
        map.put("fbaStorageList", fbaStorageList);

        List<Map> statusList = (List<Map>) tailTrucksService.updateStatusInit().getData();
        map.put("statusList", statusList);

        List<StorageListRes> storageList = storageService.getList(new HashMap<>());
        map.put("storageList", storageList);

        return SingleResult.success(map);
    }

    @Override
    public SingleResult getInfoByDate(String startDate, String endDate, String storageId) {
        List<Map> result = new ArrayList<>();


        List<Map> statusList = (List<Map>) tailTrucksService.updateStatusInit().getData();
        for (Map map : statusList) {
            LambdaQueryWrapper<TailTrucksDo> wrapper = new LambdaQueryWrapper<>();
            wrapper.ge(TailTrucksDo::getCreateTime, startDate)
                    .le(TailTrucksDo::getCreateTime, endDate)
                    .eq(TailTrucksDo::getStorageId, storageId);
            wrapper.eq(TailTrucksDo::getStatus, map.get("id"));
            List<TailTrucksDo> tailTrucksDos = tailTrucksService.getBaseMapper().selectList(wrapper);
            List<Map> mapList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tailTrucksDos)) {
                for (TailTrucksDo tailTrucksDo : tailTrucksDos) {
                    Map map1 = new HashMap();
                    map1.put("id", tailTrucksDo.getId());
                    map1.put("value", tailTrucksDo.getTrucksNumber());
                    mapList.add(map1);
                }
            }
            map.put("list", mapList);
            result.add(map);
        }
        return SingleResult.success(result);
    }

    @Override
    public SingleResult detail(Long id) {
        TailTrucksDo tailTrucksDo = tailTrucksService.getBaseMapper().selectById(id);
        TailTrucksPageRes res = new TailTrucksPageRes();
        BeanUtils.copyProperties(tailTrucksDo, res);
        FbaStorageDo fbaStorageDo = fbaStorageDao.selectById(tailTrucksDo.getFbaStorageId());
        if (fbaStorageDo != null) {
            res.setFbaStorage(fbaStorageDo.getFbaCode());
        }
        TrucksTemplateDo trucksTemplateDo = trucksTemplateDao.selectById(tailTrucksDo.getTrucksId());
        if (trucksTemplateDo != null) {
            res.setTrucks(trucksTemplateDo.getTrucksCode());
        }
        res.setStatus(getStatusStr(tailTrucksDo.getStatus()));
        return SingleResult.success(res);
    }

    private String getStatusStr(Integer status) {
        if (status == 1) {
            return "待确定";
        } else if (status == 2) {
            return "待约车";
        } else if (status == 3) {
            return "已约车";
        } else if (status == 4) {
            return "已配送";
        }
        return "";
    }

}
